package com.aquarium.presentation;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Modality;
import javafx.stage.Stage;

import com.aquarium.application.GererContientCtrl;
import com.aquarium.application.GererEspeceCtrl;
import com.aquarium.application.HelperException;
import com.aquarium.application.NicheAPoissonsException;
import com.aquarium.metier.ContientDTO;

public class ModifierContient implements Initializable {

	@FXML private Button b_annuler;
	@FXML private Button b_modifier;
	@FXML private Button b_supprimer;
	@FXML private TextField tf_nb_poisson;
	@FXML private TextField tf_nom_espece;
	
	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		
		
		
		try {
			tf_nom_espece.setText(new GererEspeceCtrl().retreiveObj(ListerParAquarium.getModifContient().getEspe_id()).getEspe_nom());
		} catch (HelperException | NicheAPoissonsException e) {
			new MessageFrame(new Label(e.getMessage()), "Erreur", Modality.APPLICATION_MODAL).showAndWait();
			//System.out.println("retreive has failed");
		}
		tf_nom_espece.setDisable(true);
		
		tf_nb_poisson.setText(String.valueOf(ListerParAquarium.getModifContient().getConti_nb_pois()));

		b_supprimer.setOnAction(new EventHandler<ActionEvent>() {
			
			@Override
			public void handle(ActionEvent arg0) {
				try {
					new GererContientCtrl().deleteObj(ListerParAquarium.getModifContient().getAqua_id(),
							ListerParAquarium.getModifContient().getEspe_id());
				} catch (NicheAPoissonsException e) {
					new MessageFrame(new Label(e.getMessage()), "Erreur Suppresion Espece", Modality.APPLICATION_MODAL).showAndWait();

					//e.printStackTrace();
				}
				
				ListerParAquarium.getTv_listerParAqua().getItems().clear();
				try {
					ListerParAquarium.getTv_listerParAqua().setItems(FXCollections.observableArrayList(
							new GererContientCtrl().retreiveAllObjByIdAqua(
									ListerParAquarium.getCb_aquaId().getValue())));
				} catch (NicheAPoissonsException e) {
					new MessageFrame(new Label(e.getMessage()), "Erreur recuperation des Especes", Modality.APPLICATION_MODAL).showAndWait();

					//e.printStackTrace();
				} catch (HelperException e) {
					new MessageFrame(new Label(e.getMessage()), "Erreur recuperation des Especes", Modality.APPLICATION_MODAL).showAndWait();

					//e.printStackTrace();
				}
				ListerParEspece.updatePanel();
				Node  source = (Node)  arg0.getSource();
			    Stage stage  = (Stage) source.getScene().getWindow();
			    stage.close();
				
			}
		});
		
		b_modifier.setOnAction(new EventHandler<ActionEvent>() {
			
			@Override
			public void handle(ActionEvent arg0) {
				try{
				new GererContientCtrl().updateObj(new ContientDTO(ListerParAquarium.getCb_aquaId().getValue(),
						(new GererEspeceCtrl().retreiveObjByNom(tf_nom_espece.getText())).getEspe_id(),
						tf_nb_poisson.getText()));
				}catch(NicheAPoissonsException | HelperException e){
					new MessageFrame(new Label(e.getMessage()), "Erreur de saisi", Modality.APPLICATION_MODAL).showAndWait();

				}
				
				ListerParAquarium.getTv_listerParAqua().getItems().clear();
				try {
					ListerParAquarium.getTv_listerParAqua().setItems(FXCollections.observableArrayList(
							new GererContientCtrl().retreiveAllObjByIdAqua(
									ListerParAquarium.getCb_aquaId().getValue())));
				} catch (NicheAPoissonsException e) {
					new MessageFrame(new Label(e.getMessage()), "Erreur recuperation des Especes", Modality.APPLICATION_MODAL).showAndWait();

					//e.printStackTrace();
				} catch (HelperException e) {
					new MessageFrame(new Label(e.getMessage()), "Erreur recuperation des Especes", Modality.APPLICATION_MODAL).showAndWait();

					//e.printStackTrace();
				}
				ListerParEspece.updatePanel();
				Node  source = (Node)  arg0.getSource();
			    Stage stage  = (Stage) source.getScene().getWindow();
			    stage.close();	
				
			}
		});
		
		b_annuler.setOnAction(new EventHandler<ActionEvent>() {
			
			@Override
			public void handle(ActionEvent arg0) {
				Node  source = (Node)  arg0.getSource();
			    Stage stage  = (Stage) source.getScene().getWindow();
			    stage.close();	
				
			}
		});
		
	}

}
